﻿package com.yy.tools.string
{
	import flash.utils.ByteArray;

/**
 *  The StringUtil utility class is an all-static class with methods for
 *  working with String objects within Flex.
 *  You do not create instances of StringUtil;
 *  instead you call methods such as 
 *  the <code>StringUtil.substitute()</code> method.  
 */
	public class StringUtil
	{
	
		/**
		 * 去首尾空白字符
		 * @param str
		 * @return 
		 * 
		 */
	    public static function trim(str:String):String
	    {
	        if (str == null) return '';
	        
	        var startIndex:int = 0;
	        while (isWhitespace(str.charAt(startIndex)))
	            ++startIndex;
	
	        var endIndex:int = str.length - 1;
	        while (isWhitespace(str.charAt(endIndex)))
	            --endIndex;
	
	        if (endIndex >= startIndex)
	            return str.slice(startIndex, endIndex + 1);
	        else
	            return "";
	    }
	    
		/**
		 *去掉分隔符两边的空白字符 
		 * @param value
		 * @param delimiter
		 * @return 
		 * 
		 */
	    public static function trimArrayElements(value:String, delimiter:String):String
	    {
	        if (value != "" && value != null)
	        {
	            var items:Array = value.split(delimiter);
	            
	            var len:int = items.length;
	            for (var i:int = 0; i < len; i++)
	            {
	                items[i] = StringUtil.trim(items[i]);
	            }
	            
	            if (len > 0)
	            {
	                value = items.join(delimiter);
	            }
	        }
	        
	        return value;
	    }
		/**
		 *判断是否是空白字符 
		 * @param character
		 * @return 
		 * 
		 */	
	    public static function isWhitespace(character:String):Boolean
	    {
	        switch (character)
	        {
	            case " ":
	            case "\t":
	            case "\r":
	            case "\n":
	            case "\f":
	                return true;
	
	            default:
	                return false;
	        }
	    }
	
		/**
		 *替换字符串   
		 * @param str
		 * @param rest
		 * @return 
		 * eg:
		 * trace(substitute("111{0}2222{1}333", "a", "b");
		 * //输出 111{a}2222{b}333
		 * trace(substitute("111{0}2222{1}333", ["a", "b"]);
		 * //输出 111{a}2222{b}333
		 */
	    public static function substitute(str:String, ... rest):String
	    {
	        if (str == null) return '';
	        
	        var len:uint = rest.length;
	        var args:Array;
	        if (len == 1 && rest[0] is Array)
	        {
	            args = rest[0] as Array;
	            len = args.length;
	        }
	        else
	        {
	            args = rest;
	        }
	        
	        for (var i:int = 0; i < len; i++)
	        {
	            str = str.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
	        }
	
	        return str;
	    }
		
		/**
		 * 生成 颜色 文本
		 * @param str
		 * @param color
		 * @return 
		 * 
		 */
		public static function getColorText(str:String, color:uint):String {
			return str = "<font color='#" + color.toString(16) +"'>" + str + "</font>";
		}
		
		/**
		 * 转译html的<>标签
		 * @param str
		 * @return 
		 * 
		 */
		public static function processHtmlChar(str:String):String {
			str = str.replace(/</g, "&lt;");
			str = str.replace(/>/g, "&gt;");
			return str;
		}
		
		
		/**
		 * 用数组中的元素一次替换特殊字符
		 * str--要处理的str
		 * replaceStr--str中要被替换的字符串
		 * refArr--用来替换str中的特殊字符
		 */
		public static function replaceSring(str:String,replaceStr:String, refArr:Array):String {
			var arr:Array = str.split(replaceStr);
			if(arr.length <= 1) {
				return str;
			}
			var j:int = 0;
			var tempStr:String = "";
			for(var i:int=0; i<arr.length-1; i++) {
				tempStr += arr[i];
				arr.splice(i+1, 0, refArr[j++]);
				i++;
				tempStr += arr[i];
			}
			tempStr += arr[arr.length-1];
			return tempStr;
		}
		public static function getTimerFormat(_arg1:int):String{
			var _local2:String;
			var _local3:Array;
			var _local4:int;
			var _local5:String;
			var _local6:int;
			_arg1 = Math.abs(Number(_arg1));
			_local2 = "";
			_local3 = [3600, 60, 1];
			_local4 = 0;
			while (_local4 < 3) {
				_local5 = ((_local4)==2) ? "" : ":";
				_local6 = (_arg1 / _local3[_local4]);
				if ((((_local6 == 0)) && ((_local4 == 0)))){
				} else {
					_arg1 = (_arg1 - (_local6 * _local3[_local4]));
					if (_local6 < 10){
						_local2 = (_local2 + (("0" + _local6.toString()) + _local5));
					} else {
						_local2 = (_local2 + (_local6.toString() + _local5));
					};
				};
				_local4++;
			};
			return (_local2);
		}
		/**
		 *将美元格式转换成数字 
		 * @param str
		 * @return 
		 * 
		 */
		public static function getDollarFormatValue(str:String):Number{
			str = str.replace(/\$|,|\s*/g, "");
			return (Number(str));
		}
		
		/**
		 *将数字转换成美元 输入格式 
		 * @param numb
		 * @param boo 是否在前面加上美元字符
		 * @return 
		 * eg  trace(getDollarFormat(12345))
		 * //输出 $12,345
		 */
		public static function getDollarFormat(numb:Number, boo:Boolean=true):String{
			var str:String;
			var mod:int;
			var temp:String;
			str = numb.toString();
			mod = (str.length % 3);
			temp = str.substr(0, mod);
			if ((str.length > 3 && (str.length % 3) != 0)){
				temp = (temp + ",");
			};
			str = str.substr(mod);
			//加入字分隔符号
			str = str.replace(/\d{3}/g, "$&,");
			if (boo){
				str = (("$" + temp) + str.substr(0, (str.length - 1)));
			} else {
				str = (temp + str.substr(0, (str.length - 1)));
			};
			return str;
		}
		
		/**
		 * 将大数字转换成 有单位的数字表示
		 * @param numb 要转换的数字
		 * @param accuracy 保留小数点
		 * @return 
		 *trace( getWorldMoneyFormat(123456))
		 * //输出 123k
		 */
		public static function getWorldMoneyFormat(numb:Number, accuracy:uint=0):String{
			var returnStr:String;
			var boo:Boolean;
			var arr:Array;
			var arr2:Array;
			var len:int;
			returnStr = "";
			boo = false;
			arr = [1000, 1000000, 1000000000];
			arr2 = ["K", "M", "B"];
			len = (arr.length - 1);
			while (len >= 0) {
				if (numb < arr[len]){
				} else {
					boo = true;
					break;
				};
				len--;
			};
			if (boo){
				returnStr = (((numb / arr[len]) as Number).toFixed(accuracy) + arr2[len]);
			} else {
				returnStr = numb.toString();
			};
			return (returnStr);
		}
		
		public static function getStringLengthByCodes(input:String,charSet:String="gb2312"):int{
			var byte:ByteArray = new ByteArray();
			byte.writeMultiByte(input,charSet);
			byte.position = 0;
			return byte.bytesAvailable;
		}
		
		public static function getTextByCharLength(input:String,length:int,charSet:String="gb2312"):String{
			if(length<1||input==null)return "";
			var byte:ByteArray = new ByteArray();
			byte.writeMultiByte(input,charSet);
			byte.position = 0;
			return byte.readMultiByte(Math.min(length,byte.bytesAvailable),"gb2312");			
		}
	}
}
